package org.stvd.service.message.impl;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.common.SecurityUserHolder;
import org.stvd.common.enums.MessageStatusEnum;
import org.stvd.common.enums.MessageTypeEnum;
import org.stvd.common.utils.ServiceResult;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.DateUtil;
import org.stvd.core.util.IdWorker;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.message.BsMessageInfo;
import org.stvd.entities.message.BsMessageLog;
import org.stvd.repository.message.BsMessageInfoDao;
import org.stvd.repository.message.BsMessageLogDao;
import org.stvd.service.base.impl.BaseServiceImpl;
import org.stvd.service.message.BsMessageInfoService;

/**
 * @Title: BsMessageInfoServiceImpl
 * @Description: 消息内容业务层实现类
 * @author houzx
 * @date 2020年9月18日
 */
@Service("BsMessageInfoService")
public class BsMessageInfoServiceImpl extends BaseServiceImpl<BsMessageInfo> 
    implements BsMessageInfoService{
    
    @Resource(name = "BsMessageInfoDao")
    private BsMessageInfoDao bsMessageInfoDao;
    @Resource(name = "BsMessageLogDao")
    private BsMessageLogDao bsMessageLogDao;

    @Override
    public QueryResult<BsMessageInfo> querySendMessageResult(int start, int limit, 
        Date queryStartDate, Date queryEndDate, String title, String sendId) {
        return bsMessageInfoDao.querySendMessageResult(start, limit, 
            queryStartDate, queryEndDate, title, sendId);
    }
    
    @Override
    @Transactional
    public ServiceResult<Map<String, Object>> sendPrivateMessage(String recId, String message) {
        return sendPrivateMessage(recId, "-1", message);
    }
    
    @Override
    @Transactional
    public ServiceResult<Map<String, Object>> sendPrivateMessage(String recId, String sendId, String message) {
        ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String,Object>>();
        if(StringUtil.isEmpty(recId) || StringUtil.isEmpty(sendId) || StringUtil.isEmpty(message)) {
            serviceResult.setParamError();
        }
        BsMessageInfo bsMessageInfo = new BsMessageInfo();
        bsMessageInfo.setId(IdWorker.getInstance().nextId());
        bsMessageInfo.setType(MessageTypeEnum.privateMessage.getValue());
        bsMessageInfo.setTitle(MessageTypeEnum.privateMessage.getText());
        bsMessageInfo.setContent(message);
        bsMessageInfo.setCreateUser(sendId);
        bsMessageInfo.setCreateTime(DateUtil.getSystemDate());
        
        BsMessageLog bsMessageLog = new BsMessageLog();
        bsMessageLog.setId(IdWorker.getInstance().nextId());
        bsMessageLog.setRecId(recId);
        bsMessageLog.setMessageId(bsMessageInfo.getId());
        bsMessageLog.setStatus(MessageStatusEnum.UnRead.getValue());
        
        bsMessageInfoDao.insert(bsMessageInfo);
        bsMessageLogDao.insert(bsMessageLog);
        
        serviceResult.setMessage("发送成功！");
        return serviceResult;
    }

    @Override
    public ServiceResult<Map<String, Object>> sendPublicMessage(String sendId, String title, String message) {
        ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String,Object>>();
        if(StringUtil.isEmpty(sendId) || StringUtil.isEmpty(title) || StringUtil.isEmpty(message)) {
            serviceResult.setParamError();
        }
        BsMessageInfo bsMessageInfo = new BsMessageInfo();
        bsMessageInfo.setId(IdWorker.getInstance().nextId());
        bsMessageInfo.setType(MessageTypeEnum.publicMessage.getValue());
        bsMessageInfo.setTitle(title);
        bsMessageInfo.setContent(message);
        bsMessageInfo.setCreateUser(sendId);
        bsMessageInfo.setCreateTime(DateUtil.getSystemDate());
        bsMessageInfoDao.insert(bsMessageInfo);
        serviceResult.setMessage("发送成功！");
        return serviceResult;
    }

    @Override
    public ServiceResult<Map<String, Object>> sendSystemMessage(String title, String message) {
        ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String,Object>>();
        if(StringUtil.isEmpty(title) || StringUtil.isEmpty(message)) {
            serviceResult.setParamError();
        }
        BsMessageInfo bsMessageInfo = new BsMessageInfo();
        bsMessageInfo.setId(IdWorker.getInstance().nextId());
        bsMessageInfo.setType(MessageTypeEnum.systemMessage.getValue());
        bsMessageInfo.setTitle(title);
        bsMessageInfo.setContent(message);
        bsMessageInfo.setCreateUser("-1");
        bsMessageInfo.setCreateTime(DateUtil.getSystemDate());
        bsMessageInfoDao.insert(bsMessageInfo);
        serviceResult.setMessage("发送成功！");
        return serviceResult;
    }

    @Override
    @Transactional
    public ServiceResult<Map<String, Object>> insertMessageInfo(BsMessageInfo bsMessageInfo) {
        ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String,Object>>();
        if(bsMessageInfo==null) {
            serviceResult.setParamError();
            return serviceResult;
        }
        String userId = SecurityUserHolder.getCurrentUserid();
        if(MessageTypeEnum.privateMessage.getValue().equals(bsMessageInfo.getType())) {
            serviceResult = sendPrivateMessage(bsMessageInfo.getRecId(), userId, bsMessageInfo.getContent());
        }else if(MessageTypeEnum.publicMessage.getValue().equals(bsMessageInfo.getType())) {
            serviceResult =  sendPublicMessage(userId, bsMessageInfo.getTitle(), bsMessageInfo.getContent());
        }
        return serviceResult;
    }
}
